package com.atguigu.gmall.oms.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.common.bean.ResponseVo;
import com.atguigu.gmall.oms.entity.OrderItemEntity;
import com.atguigu.gmall.oms.feign.GmallPmsClient;
import com.atguigu.gmall.oms.mapper.OrderItemMapper;
import com.atguigu.gmall.oms.vo.OrderItemVo;
import com.atguigu.gmall.oms.vo.OrderSubmitVo;
import com.atguigu.gmall.pms.entity.*;
import com.atguigu.gmall.pms.vo.SaleAttrValueVo;
import com.atguigu.gmall.ums.entity.UserAddressEntity;
import jdk.nashorn.internal.runtime.JSONFunctions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.common.bean.PageParamVo;

import com.atguigu.gmall.oms.mapper.OrderMapper;
import com.atguigu.gmall.oms.entity.OrderEntity;
import com.atguigu.gmall.oms.service.OrderService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;


@Service("orderService")
public class OrderServiceImpl extends ServiceImpl<OrderMapper, OrderEntity> implements OrderService {

    @Autowired
    private OrderItemMapper itemMapper;

    @Autowired
    private GmallPmsClient pmsClient;

    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<OrderEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<OrderEntity>()
        );

        return new PageResultVo(page);
    }

    @Override
    public void saveOrder(OrderSubmitVo submitVo, Long userId) {
        //保存订单
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setOrderSn(submitVo.getOrderToken()); //订单号
        orderEntity.setCreateTime(new Date()); //订单创建时间
        orderEntity.setUserId(userId); //用户id
        orderEntity.setTotalAmount(submitVo.getTotalPrice()); //总价
        orderEntity.setPayAmount(submitVo.getTotalPrice()); //应付总额
        orderEntity.setIntegrationAmount(new BigDecimal(submitVo.getBounds() / 100));
        orderEntity.setPayType(submitVo.getPayType()); //支付类型
        orderEntity.setSourceType(0); //订单来源
        orderEntity.setStatus(0); //订单状态
        orderEntity.setDeliveryCompany(submitVo.getDeliveryCompany()); //物流配送
        UserAddressEntity address = submitVo.getAddress();
        if (address != null) {
            orderEntity.setReceiverRegion(address.getRegion());
            orderEntity.setReceiverProvince(address.getProvince());
            orderEntity.setReceiverPostCode(address.getPostCode());
            orderEntity.setReceiverPhone(address.getPhone());
            orderEntity.setReceiverName(address.getName());
            orderEntity.setReceiverCity(address.getCity());
            orderEntity.setReceiverAddress(address.getAddress());
        }
        orderEntity.setDeleteStatus(0);
        orderEntity.setUseIntegration(submitVo.getBounds()); //积分抵扣
        this.save(orderEntity);
        Long orderId = orderEntity.getId();
        //保存订单详情
        List<OrderItemVo> items = submitVo.getItems();
        items.forEach(item->{
            saveOrderItem(orderId,submitVo.getOrderToken(),item);
        });
    }

    private void saveOrderItem(Long orderId, String orderToken,OrderItemVo item) {
        OrderItemEntity orderItemEntity = new OrderItemEntity();
        orderItemEntity.setOrderId(orderId);
        orderItemEntity.setOrderSn(orderToken);
        SkuEntity skuEntity = pmsClient.querySkuById(item.getSkuId()).getData();
        orderItemEntity.setSkuName(skuEntity.getName());
        orderItemEntity.setSkuPrice(skuEntity.getPrice());
        orderItemEntity.setSkuId(skuEntity.getId());
        orderItemEntity.setSkuPic(skuEntity.getDefaultImage());
        //保存商品图片
        SpuDescEntity spuDescEntity = pmsClient.querySpuDescById(skuEntity.getSpuId()).getData();
        //保存sku销售属性信息
        List<SaleAttrValueVo> saleAttrValueVos = pmsClient.querySkuSaleAtrrBySpuId(item.getSkuId()).getData();
        orderItemEntity.setSkuAttrsVals(JSON.toJSONString(saleAttrValueVos));
        orderItemEntity.setSpuPic(spuDescEntity.getDecript());
        //品牌名称
        BrandEntity brandEntity = pmsClient.queryBrandById(skuEntity.getBrandId()).getData();
        SpuEntity spuEntity = pmsClient.querySpuById(skuEntity.getSpuId()).getData();
        orderItemEntity.setSpuId(spuEntity.getId());
        orderItemEntity.setSpuName(spuEntity.getName());
        orderItemEntity.setSpuBrand(brandEntity.getName());
        orderItemEntity.setCategoryId(skuEntity.getCategoryId());
        orderItemEntity.setSkuQuantity(item.getCount().intValue());
        itemMapper.insert(orderItemEntity);
    }


}